package io.renren.common.jnkt;

import com.sun.jna.ptr.ByteByReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.io.FileOutputStream;
import java.io.OutputStream;

import static io.renren.common.jnkt.JnktSdkTest.Time2Str;

/**
 * @author: macy
 * @Email: 420534221@qq.com
 * @Date: 2020/8/5
 * @describe:
 */
@Component
@Order(1) // 加载顺序
public class MyCommandLineRunner implements CommandLineRunner{
    private static final Logger log = LoggerFactory.getLogger(MyCommandLineRunner.class);


    public static int ipNum_max = 20;
    public static String[] ipStr = new String[ipNum_max];
    public static String dataBase="D:/data/temp/";



    @Override
    public void run(String... args) throws Exception {
        try {
            int res=0;
            //指定jna编码格式
            System.setProperty("jna.encoding","GBK");
            //设置保存软加密注册码文件的路径
            JnktSdkTest.KeyTop_SetSoftEncryptPath(dataBase);

            //打印 SDK 版本号
            log.debug("SDK 版本号："+JnktSdkTest.KeyTop_GetSDKVersion());
            //开启 写日志
            JnktSdkTest.KeyTop_EnableSaveLog(1,dataBase);
            //开启保存图片
            JnktSdkTest.KeyTop_EnableSaveImage(0, dataBase);
            //设置通讯方式 1-IP通讯 2-ID通讯
            JnktSdkTest.KeyTop_SetCommunicationWay(1);
            //绑定端口
            res=JnktSdkTest.KeyTop_EnableNetwork(5001);
            if(res!=0){
                log.debug("端口绑定识别 code:"+res);
            }else{
                log.debug("网络初始化成功并绑定端口："+5001);
            }

            /**
             * 设置开启或关闭相机日志接收模块
             **/
            JnktSdkTest.KeyTop_EnableSaveLog(1);

            // 初始化相机占位
            for(int k = 0 ;k < ipStr.length ;k++){
                ipStr[k] = "255.255.255.255";
            }



            //车牌回调 报名(上学前)    (注册)
            JNADll.IPlateCallback plateCallback = new PlateCallback();
            JNADll.INSTANCE.InitKTRecvImageCallbackFun(plateCallback);

            //信号或状态查询回调
            JNADll.IPSignalAndState signalAndState = new SignalCallback();
            JNADll.INSTANCE.InitKTQueryDeviceStateCallbackFun(signalAndState);


            log.debug("相机SDK 初始化完成");

        }catch (Exception e){
            log.error("初始化相机SDK 出错啦:"+e.getMessage());
        }


    }


    /*
     * @车牌结果回调函数以及保存图片    期末考  (才能知道上课的效果)   (车牌识别结果以及图片数据回传)  (注册一定要这个之前)
     */
    public  class PlateCallback implements JNADll.IPlateCallback {
        /**
         *
         * @param ip  相机IP
         * @param dataBuf
         * @param dataLen  数据长度
         * @param fileName 文件名称
         * @param carColor 颜色
         * @param carPlate 车牌号
         * @param plateReliability  可信度
         * @param seriaNo
         * @param imageType
         * @param left
         * @param top
         * @param right
         * @param bottom
         */
        public void apply(String ip, ByteByReference dataBuf, int dataLen, String fileName,
                          int carColor, String carPlate, int plateReliability, String seriaNo,
                          int imageType, int left, int top, int right, int bottom) {


            log.debug("相机IP:"+ip+",File:"+fileName+",Color:"+carColor+",carPlate:"+carPlate+",Reliability:"+plateReliability+",Area:{"+left+","+top+","+right+","+bottom+"};");

            String ImageName = Time2Str()+"_"+carPlate+".jpg";
            if (dataLen > 0){//存储成图片
                System.out.println(dataBase+ImageName);
                SaveImage(dataBuf.getPointer().getByteArray(0, dataLen),dataBase+ImageName);

                //识别到就开闸
                JnktSdkTest.KeyTop_OpenGate(ip);
            }
        }
    }

    public  class SignalCallback implements JNADll.IPSignalAndState {
        /**
         *
         * @param ip
         * @param BarrierGate
         * @param SenseCoil
         */
        public void Query1111(String ip,ByteByReference BarrierGate,ByteByReference SenseCoil) {

            log.debug("道闸升信号:"+BarrierGate.getPointer().getByte(0));
            log.debug("道闸降信号:"+BarrierGate.getPointer().getByte(1));
            log.debug("道闸升到位信号:"+BarrierGate.getPointer().getByte(2));
            log.debug("道闸降到位信号:"+BarrierGate.getPointer().getByte(3));
            log.debug("道闸触发地感状态:"+SenseCoil.getPointer().getByte(0));
            log.debug("道闸防砸地感状态:"+SenseCoil.getPointer().getByte(1));
        }
    }


    /*
     * @保存图片数据成文档
     */
    public  void SaveImage(byte[] data_Image,String path_Image){
        try{
            OutputStream out = new FileOutputStream(path_Image);
            out.write(data_Image);
            out.flush();
            out.close();
        }catch(Exception e){
            e.printStackTrace();
        }
    }


}
